﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using MINERP.BLL.InfoConfig;
using MINERP.DAL;
using MINERP.MODEL.InfoConfig;


namespace MINERP.InfoConfig.Controller
{
    public class ShopController
    {
        #region //private fields
        private PaperFaceController _paperFaceController = new PaperFaceController();
        private PaperTypeController _paperTypeController = new PaperTypeController();
        private ProductController   _productController = new ProductController();
        private ShopBLL _bll = new ShopBLL();
        private GridControl _grid = null;
        private GridView _view = null;
        private ComboBoxEdit _comboBox = null;
        private GroupControl _groupControl = null;
        private CheckEdit _checkEdit = null;
        private List<ProductEntity> _productList = null;

        #endregion


        #region //Public Properties

        internal List<ProductEntity> ProductList
        {
            get { return _productList; }
            set { _productList = value; }
        }
        internal GroupControl GroupControl
        {
            set
            {
                _groupControl = value;
            }
        }
        internal CheckEdit CheckEdit
        {
            set
            {
                _checkEdit = value;
            }
        }
        internal ComboBoxEdit ComoBox
        {
            set
            {
                _comboBox = value;
            }
        }
        public GridControl Grid
        {
            set
            {
                _grid = value;
            }
        }

        public GridView View
        {
            set
            {
                this._view = value;
            }
            get
            {
                return _view;
            }
        }

        #endregion

        #region //Public Methods

        /// <summary>
        /// Get shops when the form loadding
        /// </summary>
        public void GetDate(int rowHandle) 
        {
            _grid.DataSource = _bll.GetShopList();
            _view.FocusedRowHandle = rowHandle;
        }
        public void GetShopData()
        {
            List<ShopEntity> list = new List<ShopEntity>();
            foreach (var item in _bll.GetShopList())
            {
                ShopEntity shopEntity = new ShopEntity();
                shopEntity.address = item.address;
                shopEntity.Balance =item.Balance ==null?0:(decimal)item.Balance;
                shopEntity.OrderBalance =item.OrderBalance==null?0:(decimal)item.OrderBalance;
                shopEntity.CheckOutType =item.CheckOutType==null?4:(int)item.CheckOutType;
                shopEntity.CreditLimit =item.CreditLimit==null?0:(int)item.CreditLimit;
                shopEntity.mnemonicCode = item.mnemonicCode;
                shopEntity.ShopId = item.ShopId;
                shopEntity.shopKeeper = item.shopKeeper;
                shopEntity.shopName = item.shopName;
                shopEntity.Tel1 = item.Tel1;
                shopEntity.Tel2 = item.Tel2;
                switch (item.CheckOutType)
                {
                    case 0:
                        shopEntity.CheckOutTypeName = "预付款";
                        break;
                    case 1:
                        shopEntity.CheckOutTypeName = "月结";
                        break;
                    case 2:
                        shopEntity.CheckOutTypeName = "先结后做";
                        break;
                    case 3:
                        shopEntity.CheckOutTypeName = "先做后结";
                        break;
                    default:
                        break;

                }
                list.Add(shopEntity);
            }
            _grid.DataSource = list;
        }
        /// <summary>
        /// Get shop object by shop id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Shop GetShopObj(int id)
        {
            return _bll.GetShopObj(id);
        }

        public Shop GetShopObj(string name)
        {
            return _bll.GetShopObj(name);
        }
        /// <summary>
        /// Add shops
        /// </summary>
        /// <param name="obj"></param>
        public void AddShop(Shop obj,string ftpLoginId,string ftpPwd)
        {
            List<ProductEntity> list = _productList.Where(p => p.IsChecked == true).ToList();
            _bll.AddShop(obj, list,ftpLoginId, ftpPwd);
        }
        
        /// <summary>
        /// Update shop
        /// </summary>
        /// <param name="obj"></param>
        public void UpdateShop(Shop obj,decimal areaPrice, shop_DistributionInfo shop_DistributionInfo)
        {
            _bll.UpdateShop(obj,areaPrice,_productList, shop_DistributionInfo);
        }

        /// <summary>
        /// Delete shop
        /// </summary>
        /// <param name="obj"></param>
        public void DeleteShop(Shop obj)
        {
            _bll.DeleteShop(obj);
        }


        /// <summary>
        /// 绑定纸面
        /// </summary>
        public void LinkcmbPaperFace()
        {
            PaperFaceBLL bll = new PaperFaceBLL();
            List<paperFace> list = bll.GetPaperFaceList();
            foreach (paperFace obj in list)
            {
                if (obj.Name != "任意")
                {
                    _comboBox.Properties.Items.Add(obj.Name);
                }
            }
        }
        /// <summary>
        /// 绑定纸型
        /// </summary>
        public void LinkcmbPaperType()
        {
            PaperTypeBLL bll = new PaperTypeBLL();
            List<paperType> list = bll.GetPaperTypeList();
            foreach (paperType obj in list)
            {
                if (obj.Name != "任意")
                {
                    _comboBox.Properties.Items.Add(obj.Name);
                }
            }

        }

        public void LinkTech()
        {
            TechBLL techbll = new TechBLL();
            foreach (var item in techbll.GetTechList())
            {
                _comboBox.Properties.Items.Add(item.TechName);
            }
        }
        public void LinkUser()
        {
            UserBLL userbll = new UserBLL();
            foreach (var item in userbll.GetUserList())
            {
                _comboBox.Properties.Items.Add(item.UserName);
            }
        }
        public SUUsers GetSUUser(Shop shop)
        {
            return _bll.GetSUUser(shop);
        }
        public void SetFtp(bool flag,Shop shop,string ftpLoginId,string ftpPwd,string shopLoginid,string shoploginPwd)
        {
            _bll.SetFtp(flag, shop,ftpLoginId,ftpPwd,shopLoginid,shoploginPwd);
        }

        public bool ExportProductToXml(Shop shop)
        {
            return _bll.ExportProductsToXml(shop);
        }
        /// <summary>
        /// 检查ftp账号是否存在
        /// </summary>
        /// <param name="ftpLoginId"></param>
        /// <returns></returns>
        public bool IsFtpLoginIdExist(string ftpLoginId,Shop shop)
        {
            return _bll.IsFtpLoginIdExist(ftpLoginId,shop);
        }
        /// <summary>
        /// 检查商户远程登录账号是否存在
        /// </summary>
        /// <param name="shopLoginId"></param>
        /// <returns></returns>
        public bool IsShopLoginIdExist(string shopLoginId,Shop shop)
        {
            return _bll.IsShopLoginIdExist(shopLoginId,shop);
        }
        public void GetProductlist(Shop shop)
        {
            _productList = _bll.GetProductList(shop);
            _grid.DataSource = _productList;
        }
        public void GetProductlist()
        {
            _productList = _bll.GetProductList();
            _grid.DataSource = _productList;
        }

        internal void ResumeShop(Shop _shop)
        {
            _bll.Resume(_shop);
        }
        #endregion


    }
}
